<template>
	<view class="footprint">
		<!-- <view class="tips">向左滑动移除收藏</view> -->
		<view class="container" v-if="browsinghistory.length>0">
			<view class="date-card" v-for="(item,index) in browsinghistory" :key="index">
				<view class="date">{{item.browsingDate}}</view>
				<view class="goods-list">
					<view class="goods-item" v-for="(record,indexd) in item.records" :key="indexd">
						<view v-if="record.type==0">
							<image class="thumb" :src="record.goods.logo" mode="aspectFill"></image>
							<view class="info">
								<view class="name">{{record.goods.name}}</view>
								<view class="other">
									<text class="price">￥{{record.goods.sale_price}}</text>
								</view>
							</view>
						</view>
						<view v-else-if="record.type==1">
							<image class="thumb" :src="record.store.logo" mode="aspectFill"></image>
							<view class="info">
								<view class="name">{{record.store.name}}</view>
								<view class="other">
									<text class="price">已服务{{record.store.sales_sum}}</text>
								</view>
							</view>
						</view>
						<view v-else-if="record.type==2">
							<image class="thumb" :src="record.technician.avatar" mode="aspectFill"></image>
							<view class="info">
								<view class="name">{{record.technician.name}}</view>
								<view class="other">
									<text class="price">评分{{record.technician.score}}</text>
								</view>
							</view>
						</view>
						<view v-else>
							<image class="thumb" :src="record.service.images" mode="aspectFill"></image>
							<view class="info">
								<view class="name">{{record.service.name}}</view>
								<view class="other">
									<text class="price">￥{{record.service.sale_price}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="content-card" style="text-align: center;padding: 10px 0px;" v-else>
			暂无数据
		</view>
	</view>
</template>

<script setup>
	import request from '@/utils/request';
	import checkLogin from '@/utils/login';
	import {
		ref,
		reactive,
		toRefs,
		inject
	} from 'vue';
	import {
		onLoad,
		onShow,
		onPullDownRefresh,
		onReachBottom,
	} from '@dcloudio/uni-app';

	const state = reactive({
		options: [{
			text: '移除',
			style: {
				fontSize: '28rpx',
				backgroundColor: '#dd524d'
			}
		}],
		browsinghistory: []
	});

	onLoad((opt) => {

	});

	onShow(() => {
		//登录过期自动登录
		if (!checkLogin()) {
			pageRoute('/pages/users/login/login');
		} else {
			getBrowsingHistory();
		}
	})

	onPullDownRefresh((e) => {
		console.log("下拉刷新");
	})

	onReachBottom((e) => {
		console.log("上拉加载更多", state.isMore);
		if (state.isMore) {
			getBrowsingHistory();
		}
	})

	function tabChange(e) {
		state.current = e.currentIndex;
	}

	function getBrowsingHistory() {
		request.get("browsinghistory/index", {
			skip: state.browsinghistory.length,
			limit: 10,
		}).then(res => {
			console.log('足迹列表', res);
			state.isMore = res.data.is_more;
			if (state.browsinghistory.length == 0) {
				state.browsinghistory = res.data.data;
			} else {
				state.browsinghistory = state.browsinghistory.concat(res.data.data);
			}
		})
	}

	function bindClick(id) {
		console.log('移除足迹', id);
		request.get("browsinghistory/del", {
			id: id,
		}).then(res => {
			uni.showToast({
				title: res.msg
			})
			getBrowsingHistory();
		})
	}

	function pageRoute(url) {
		uni.navigateTo({
			url
		});
	}

	const {
		options,
		browsinghistory
	} = toRefs(state);
</script>

<style lang="scss">
	.footprint {
		overflow: hidden;

		.tips {
			padding: $margin-12 $margin-24;
			background-color: mix($inverse-color, $warning-color, 95%);
			font-size: $font-24;
			color: $warning-color;
		}

		.container {
			padding: $margin-24;

			.date-card {
				padding: $margin-24 $margin-24 0;

				.date {
					font-size: $font-24;
				}

				.goods-list {
					margin-top: $margin-16;

					.goods-item {
						@include f-between;
						margin-bottom: $margin-20;
						padding: $margin-20;
						background-color: $bg-inverse-color;
						border-radius: $radius-12;

						.thumb {
							width: 128rpx;
							height: 128rpx;
							background-color: $bg-color;
							border-radius: $radius-8;
						}

						.info {
							flex: 1;
							position: relative;
							height: 128rpx;
							align-self: flex-start;
							margin-left: $margin-24;

							.name {
								@include ellipsis(2);
								font-size: $font-30;
								color: $main-color;
								font-weight: 500;
							}

							.other {
								position: absolute;
								bottom: 0;
								width: 100%;
								margin-top: $margin-12;

								.price {
									font-size: $font-32;
									color: $error-color;
								}
							}
						}
					}
				}
			}
		}
	}
</style>